//
//  MBForumViewController.m
//  MiBaoTu
//
//  Created by admin on 16/10/14.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBShowController.h"
#import "MBLoginController.h"
#import "MBRecomController.h"
#import "MBSpaceMyParentChildShowTabbarView.h"
#import "MBTheHotViewController.h"
#import "MBNewViewController.h"
#import "MBRecordController.h"
#import "MBCompetationController.h"
#import "MBTheShowViewController.h"

#import "AttentionView.h"
#import "MBShowHeaderView.h"

@interface MBShowController ()<MBSpaceMyParentChildShowTabbarViewDelegate,UIScrollViewDelegate,MBShowHeaderViewDelegate,AttentionViewDelegate> {
    MBSpaceMyParentChildShowTabbarView      *_tabbarView;      //分栏
    NSMutableArray                          *_ViewCtrlArr;
    UIScrollView                            *_scrollView;
    NSInteger                               selectTabbarIndex; //分栏选中
    
    MBShowHeaderView                        *_tableHeaderView;
    
    AttentionView                           *_attentionView;
}



@end
@implementation MBShowController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNavBar];
    [self onCreateUI];
    [self addController];
}

#pragma mark - UI
- (void)setNavBar
{
    [self setTitleView:@"亲子秀场"];
    [self setRightPlayerItem];
}

- (void)onCreateUI
{
    selectTabbarIndex = 0;
    
    //论坛最新获得图片
    _tableHeaderView = [[MBShowHeaderView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, 246*PERCEN)];
    _tableHeaderView.delegate = self;
    [self.view addSubview:_tableHeaderView];
    
    //
    _ViewCtrlArr = [[NSMutableArray alloc]initWithObjects:@"小编推荐",@"我的关注", nil];
    _tabbarView = [[MBSpaceMyParentChildShowTabbarView alloc]initWithFrame:CGRectMake(0, _tableHeaderView.bottom+30*PERCEN, SCREEN_WIDTH,(ISIPAD?100*PERCEN:80*PERCEN)) withCount:_ViewCtrlArr withbgColor:[UIColor blackColor] withSelectColor:TINK_BG_COLOR];
    _tabbarView.delegate = self;
    [self.view addSubview:_tabbarView];
    
    //全局滚动
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _tabbarView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-_tabbarView.bottom-50)];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.bounces = YES;
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(_scrollView.width*_ViewCtrlArr.count, _scrollView.height);
    
    UIButton *recordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img = [UIImage imageNamed:@"luzhi_dt"];
    [recordBtn setImage:img forState:UIControlStateNormal];
    [recordBtn setImage:img forState:UIControlStateHighlighted];
    recordBtn.frame = CGRectMake(SCREEN_WIDTH - 25*PERCEN-img.size.width, SCREEN_HEIGHT - 25*PERCEN-img.size.height-64, img.size.width, img.size.height);
    [recordBtn addTarget:self action:@selector(recodPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:recordBtn];
    [self.view bringSubviewToFront:recordBtn];
    
    _attentionView = [[AttentionView alloc]initWithTitle:@"登录之后才可以发布亲子秀哦!" withCancelBt:@"立即登录" withSureBt:@"取消"];
    _attentionView.delegate = self;
}

//添加子控件
- (void)addController
{
    for (int i=0; i<_ViewCtrlArr.count; i++) {
        MBRecomController *show = [[MBRecomController alloc]init];
        if (i==0) {
            show.sortStr = @"1";
        }
        else if (i==1) {
            show.sortStr = @"2";
        }
        
        __weak typeof(show) selfWeak = show;
        show.onRecomUIForBlock = ^ (float offSet) {
            if (offSet >= 0) {
                //隐藏
                _tableHeaderView.top = NAV_HEIGHT - offSet;
                _tabbarView.top = _tableHeaderView.bottom+30*PERCEN;
                _scrollView.top = _tabbarView.bottom;
                _scrollView.height = SCREEN_HEIGHT-_tabbarView.bottom-50;
                selfWeak.tableView.height = SCREEN_HEIGHT-NAV_HEIGHT-276*PERCEN-(ISIPAD?100*PERCEN:80*PERCEN)-50+offSet;
                
            }
        };
        [self addChildViewController:show];
        
        if (i == 0) {
            show.view.frame = CGRectMake(0, 0, _scrollView.width, _scrollView.height);
            [_scrollView addSubview:show.view];
        }
    }
}

#pragma mark - 最新、热门、活动
- (void)onSelectItemForView:(MBSpaceMyParentChildShowTabbarView *)view withIndex:(NSInteger)selectIndex
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (index != selectIndex) {
        selectTabbarIndex = selectIndex;
        [_scrollView setContentOffset:CGPointMake(selectIndex*_scrollView.width, 0) animated:YES];
    }
}

#pragma mark - 滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (index != selectTabbarIndex) {
        selectTabbarIndex = index;
        [_tabbarView onUPdateUIForSelectIndex:index];
        
        [self scrollViewDidEndScrollingAnimation:_scrollView];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    MBRecomController *show = self.childViewControllers[index];
    //UI update
    if (_tableHeaderView.top > 0 ) {
        //显示
        [show onUPdateUIForTable:SCREEN_HEIGHT-NAV_HEIGHT-276*PERCEN-(ISIPAD?100*PERCEN:80*PERCEN)-50];
    } else {
        //隐藏
        [show onUPdateUIForTable:SCREEN_HEIGHT-NAV_HEIGHT-(ISIPAD?100*PERCEN:80*PERCEN)-50];
    }
    
    if (show.view.superview) {
        return;
    }
    show.view.frame = scrollView.bounds;
    [_scrollView addSubview:show.view];
}

#pragma mark - 最新、最热、大伽秀、比赛专区、录制
//跳转到录制界面
- (void)recodPage {
    
    UserModel *model = [UserManager standerDefult].userModel;
    if (model) {
        MBRecordController *recordVC = [[MBRecordController alloc] init];
        [self.navigationController pushViewController:recordVC animated:YES];
    } else {
        [_attentionView showInWindow];
    }
}

//跳转到最新控制器
- (void)onSelectItemForTheNewWithView:(MBShowHeaderView *)view{
    
    MBNewViewController *newVC = [[MBNewViewController alloc] init];
    
    [self.navigationController pushViewController:newVC animated:YES];
    
}
//跳转到热门控制器
- (void)onSelectItemForTheHotWithView:(MBShowHeaderView *)view{
    
    MBTheHotViewController *hotVC = [[MBTheHotViewController alloc] init];
    
    [self.navigationController pushViewController:hotVC animated:YES];
    
}
//跳转到大咖秀控制器
- (void)onSelectITemForTheShowWithView:(MBShowHeaderView *)view{
    
    MBTheShowViewController *showVC = [[MBTheShowViewController alloc] init];
    
    [self.navigationController pushViewController:showVC animated:YES];
    
}
//跳转到比赛专区控制器
- (void)onSelectITemForTheCompetationWithView:(MBShowHeaderView *)view{
    
    MBCompetationController *comVC = [[MBCompetationController alloc] init];
    
    [self.navigationController pushViewController:comVC animated:YES];
    
}

#pragma mark - 弹出框
- (void)onSelectSureItem:(AttentionView *)view
{
    
}
- (void)onSelectCancelItem:(AttentionView *)view
{
    MBLoginController *registerLastVC = [[MBLoginController alloc] init];
    registerLastVC.formBaseViewCtrl = NSStringFromClass([self class]);
    [self.navigationController pushViewController:registerLastVC animated:YES];
    
}

@end
